package com.hydl.power.service.impl;

import com.hydl.power.mapper.SysRoleMapper;
import com.hydl.power.mapper.SysRoleMenuMapper;
import com.hydl.power.pojo.SysRole;
import com.hydl.power.pojo.SysRoleMenu;
import com.hydl.power.service.SysRoleService;
import com.hydl.power.utils.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wxb
 * @date 2020/3/20 14:42
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public int deleteById(Integer id) {
        return sysRoleMapper.deleteById(id);
    }

    @Override
    public int addRole(String name) {
        SysRole sysRole = new SysRole();
        sysRole.setName(name);
        return sysRoleMapper.insert(sysRole);
    }

    @Override
    public Resp saveRole(SysRole role) {
        List<Integer> menuIds = role.getMenuIds();
        int count = sysRoleMapper.insert(role);
        if(count > 0) {
            Integer roleId = role.getId();
            List<SysRoleMenu> list = new ArrayList<>();
            for (Integer menuId : menuIds) {
                SysRoleMenu roleMenu = new SysRoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menuId);
                list.add(roleMenu);
            }
            if (list.size() > 0) {
                sysRoleMapper.batchSave(list);
            }
        }
        return  Resp.success();
    }

    @Override
    public List<SysRole> getAllRole() {
        return sysRoleMapper.selectAll();
    }
}
